﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AlteraxWebControls
{
    [ToolboxData("<{0}:PagingGridView runat=server></{0}:PagingGridView>")]
    public class PagingGridView : System.Web.UI.WebControls.GridView
    {
        private int totalRows = 0;

        protected override void InitializePager(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource)
        {
            // This method is called to initialise the pager on the grid. We intercepted this and override
            // the values of pagedDataSource to achieve the custom paging using the default pager supplied
            //   if (CustomPaging)
            // {
            //pagedDataSource.AllowCustomPaging = true;
            //pagedDataSource.VirtualCount = VirtualItemCount;
            //pagedDataSource.CurrentPageIndex = CurrentPageIndex;
            // }
            if (pagedDataSource.DataSource != null)
            {
                totalRows = pagedDataSource.DataSourceCount;
                base.InitializePager(row, columnSpan, pagedDataSource);
            }
        }

        protected override void OnRowDataBound(GridViewRowEventArgs e)
        {
            base.OnRowDataBound(e);
            setGridViewPagingStyle(e);
        }

        public void setGridViewPagingStyle(System.Web.UI.WebControls.GridViewRowEventArgs e)
        {
            GridView hGrid = (GridView)this;
            if (e.Row.RowType == DataControlRowType.Pager)
            {
                TableCell origCell = (TableCell)e.Row.Controls[0];
                origCell.CssClass = hGrid.PagerStyle.CssClass;
                origCell.Style.Add("padding", "0px");
                origCell.Style.Add("margin", "0px");              
                //origCell.Style.Add("border", "none");
                //origCell.Style.Add("color", "white");
             
                Table containerTable = new Table();
                containerTable.CssClass = hGrid.PagerStyle.CssClass;
                containerTable.CellPadding = 0;
                containerTable.CellSpacing = 0;
                containerTable.Style.Add("padding", "0px");
                containerTable.Style.Add("margin", "0px");
             //   containerTable.Style.Add("border", "none");
                containerTable.Style.Add("width", "100%");

                TableRow containerRow = new TableRow();
                containerTable.Rows.Add(containerRow);
                TableCell containerCellFirst = new TableCell();
                containerCellFirst.Style.Add("width", "45%");
                containerCellFirst.CssClass = hGrid.PagerStyle.CssClass;
                containerCellFirst.Style.Add("padding", "0px");
                containerCellFirst.Style.Add("margin", "0px");
                containerCellFirst.Style.Add("text-align", "left");
                //containerCellFirst.Style.Add("border", "none");
                //containerCellFirst.Style.Add("color", "white");

                containerRow.Cells.Add(containerCellFirst);

                TableCell containerCellSecond = new TableCell();
                containerCellSecond.Style.Add("width", "55%");
                containerCellSecond.Style.Add("text-align", "right");
                containerCellSecond.CssClass = hGrid.PagerStyle.CssClass;
                containerCellSecond.Style.Add("padding", "0px");
                containerCellSecond.Style.Add("margin", "0px");
                containerCellSecond.Style.Add("text-align", "right");
                //containerCellSecond.Style.Add("border", "none");
                //containerCellSecond.Style.Add("color", "white");
                containerRow.Cells.Add(containerCellSecond);

                // Set number class seperatetly - Note:- for future extension at this point
                Table numberTable = e.Row.Cells[0].Controls[0] as Table;
                numberTable.CssClass = hGrid.PagerStyle.CssClass;
                numberTable.CellPadding = 0;
                numberTable.CellSpacing = 0;
                numberTable.Style.Add("padding", "0px");
                numberTable.Style.Add("margin", "0px");
                // numberTable.Style.Add("border", "none");                                          
               
                //-------------------------------------------------------------------
                // Create new Cell
                //-------------------------------------------------------------------
                #region new Cell
                Table newTable = new Table();
                newTable.Rows.Add(new TableRow());

                TableHeaderCell tblCel = new TableHeaderCell();
                tblCel.CssClass = hGrid.PagerStyle.CssClass;
                tblCel.Style.Add("padding-left", "10px");
                tblCel.Style.Add("margin", "0px");
                Label pageTitle = new Label();
                pageTitle.Text = "Show ";
                tblCel.Controls.AddAt(0, pageTitle);

                Label totalRowLabel = new Label();
                totalRowLabel.Text = " | Total Rows = " + totalRows.ToString();

                int iEndRecord;
                if (totalRows % this.PageSize != 0)
                    iEndRecord = (totalRows / this.PageSize) + 1;
                else
                    iEndRecord = (totalRows / this.PageSize);

                int iStartsRecods = this.PageIndex + 1;

                Label whereAreRowLabel = new Label();
                whereAreRowLabel.Text = " " + iStartsRecods.ToString() + " of " + iEndRecord.ToString();
                tblCel.Controls.Add(whereAreRowLabel);
                tblCel.Controls.Add(totalRowLabel);
                newTable.Rows[0].Controls.Add(tblCel);

                //-------------------------------------------------------------------------------
                // Add New Table and Number table into main Container Table
                //-------------------------------------------------------------------------------
                containerCellFirst.Controls.Add(newTable);
                containerCellSecond.Controls.Add(numberTable);
                e.Row.Cells[0].Controls.Remove(numberTable);             
                origCell.Controls.AddAt(0, containerTable);
                               
                #endregion              
            }
        }
    }
}
